package com.zeno.web.core.exception;

import com.zeno.soil.common.exception.BaseException;
import org.springframework.http.HttpStatus;

import java.util.Collections;

/**
 * @auth zeno
 * @date 2022/7/25 14:44
 */
public class WafException extends BaseException{

    /** http 状态码 */
    private HttpStatus status;

    /** 异常状态码 */
    private String code;


    public WafException( String msg ){
        this(HttpStatus.BAD_REQUEST, msg);
    }

    public WafException( String code, String msg ){
        this(HttpStatus.BAD_REQUEST,code, msg);
    }

    public WafException( String code, String msgPattern, Object ... msgArgs ){
        this(HttpStatus.BAD_REQUEST,code, msgPattern,msgArgs);
    }

    public WafException( String msgPattern, Object ... msgArgs ){
        this(HttpStatus.BAD_REQUEST, msgPattern,msgArgs);
    }

    /***
     *
     * @param status      http 状态码
     * @param msg         异常消息
     */
    public WafException( HttpStatus status, String msg ){
        this(status, status.name(), msg);
    }

    /***
     *
     * @param status      http 状态码
     * @param msgPattern  java.text.MessageFormat 消息模板
     * @param msgArgs     java.text.MessageFormat 消息模板参数
     */
    public WafException( HttpStatus status, String msgPattern, Object ... msgArgs ){
        this(status, status.name(), msgPattern,msgArgs);
    }

    /***
     *
     * @param status      http 状态码
     * @param code        异常状态码
     */
    public WafException( HttpStatus status, String code, String msg ){
        this(status, code, msg, Collections.emptyList());
    }

    /***
     *
     * @param status      http 状态码
     * @param code        异常状态码
     * @param msgPattern  java.text.MessageFormat 消息模板
     * @param msgArgs     java.text.MessageFormat 消息模板参数
     */
    public WafException( HttpStatus status, String code, String msgPattern, Object ... msgArgs ){
        this(status, code, null, msgPattern, msgArgs);
    }

    /***
     *
     * @param throwable   异常栈
     */
    public WafException( Throwable throwable ){
        this(HttpStatus.INTERNAL_SERVER_ERROR, HttpStatus.INTERNAL_SERVER_ERROR.name(), throwable,throwable.getMessage(), Collections.emptyList());
    }

    /***
     *
     * @param code        异常状态码
     * @param throwable   异常栈
     */
    public WafException( String code, Throwable throwable ){
        this(code, throwable,throwable.getMessage(), Collections.emptyList());
    }

    /***
     *
     * @param code        异常状态码
     * @param throwable   异常栈
     * @param msgPattern  java.text.MessageFormat 消息模板
     * @param msgArgs     java.text.MessageFormat 消息模板参数
     */
    public WafException( String code, Throwable throwable, String msgPattern, Object ... msgArgs ){
        this(HttpStatus.INTERNAL_SERVER_ERROR, code, throwable,msgPattern,msgArgs);
    }

    /***
     *
     * @param status      http 状态码
     * @param code        异常状态码
     * @param throwable   异常栈
     * @param msgPattern  java.text.MessageFormat 消息模板
     * @param msgArgs     java.text.MessageFormat 消息模板参数
     */
    public WafException( HttpStatus status, String code, Throwable throwable, String msgPattern, Object ... msgArgs ){
        super(throwable,msgPattern,msgArgs);
        this.status = status;
        this.code = code;
    }

    @Override
    public String module() {
        return "GAEA-WAF";
    }

    public HttpStatus status() {
        return status;
    }

    public String code() {
        return code;
    }
}
